<?php
/**
 * $Id: backend.php 206 2012-05-22 11:08:08Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 206 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2012-05-22 16:38:08 +0530 (Tue, 22 May 2012) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Backend helper saves time in displaying generic information / links for a particular item.
 *
 */
class BackendHelper extends AppHelper {

	var $helpers = array('Html');
	var $adminMenus = array();
	
	function addMenuItem($module, $title, $link, $adminType='super', $appendAdminPrefix = true)
	{
		if(!isset($this->Html))
		{
			$this->initHelper('Html');
		}
		if($appendAdminPrefix)
		{
			if(is_string($link))
			{
				$link = '/admin'.$link;
			}else
			{
				if(!isset($link['plugin']))
				{
					$link['plugin'] = null; // otherwise cake will add 'backend' before urls
				}
				$link['prefix'] = 'admin'; 
			}
		}
		
		$this->adminMenus[$adminType][$module][] = $this->Html->smartLink($title, $link);
	}
	
	function getAdminLinks($adminType='super')
	{
		return $this->adminMenus[$adminType];
	}
	
	function showMenu($adminType='super', $options = array())
	{
		$output = '';
		$adminLinks = $this->getAdminLinks($adminType);
		$adminSections = array(ADMIN_CONTENT, ADMIN_USERS, ADMIN_MISC, ADMIN_APPEARANCE, ADMIN_SETTINGS, ADMIN_SYSTEM);
		foreach($adminSections as $moduleTitle)
		{
			if(isset($adminLinks[$moduleTitle]) && !empty($adminLinks[$moduleTitle]))
			{
				$output .= '<li><a href="#" class="nav-top-item">'.__($moduleTitle).'</a><ul>';
				foreach($adminLinks[$moduleTitle] as $link)
				{
					$output .= '<li>'.$link.'</li>';
				}
				$output .= '</ul></li>';
			}
		}
		return $output;
	}

	/**
	 * You must set an item with this function to display its related links or status.
	 *
	 * @param unknown_type $item
	 * @param unknown_type $type
	 */
	function setItem($item, $type)
	{
		$this->item['id'] = $item[$type]['id'];
		$this->item['type'] = strtolower(Inflector::pluralize($type));
		$this->item['item'] = $item;
		if(isset($item[$type]['status']))
			$this->item['status'] = $item[$type]['status'];
	}

	/**
	 * This will output all links like view/edit/delete/block etc for the above item specified above
	 *
	 * @return unknown
	 */
	function actions()
	{
		$output = '';
		$links = func_get_args();
		foreach($links as $link)
		{
			$output .= $this->$link($this->item).' | ';
		}
		return $output;
	}

	function view($item)
	{
		$output = $this->Html->link(__('View'), '/admin/'.$item['type'].'/view/'.$item['id']);
		return $output;
	}
	function approve($item)
	{
		if($this->item['status'] == UC_STATUS_PENDING)
		{
			$output = $this->Html->link(__('Publish',true), '/admin/'.$this->item['type'].'/status/mark_approve/'.$item['id']).' | ';
			$output .= $this->Html->link(__('Mark as Spam',true), '/admin/'.$this->item['type'].'/status/mark_spam/'.$item['id']);
		}
		if($this->item['status'] == UC_STATUS_APPROVED)
		{
			$output = $this->Html->link(__('Unpublish',true), '/admin/'.$this->item['type'].'/status/mark_pending/'.$item['id']);
		}
		if($this->item['status'] == UC_STATUS_SPAM)
		{
			$output = $this->Html->link(__('Not Spam',true), '/admin/'.$this->item['type'].'/status/mark_pending/'.$item['id']);
		}
		//$output = $this->Html->link('Approve', '/'.$item['type'].'/'.$item['id']);
		return $output;
	}
	function edit($item)
	{
		$output = $this->Html->link(__('Edit'), '/admin/'.$item['type'].'/edit/'.$item['id']);
		return $output;
	}
	function delete($item)
	{
		$output = $this->Html->link(__('Delete'), '/admin/'.$item['type'].'/delete/'.$item['id']);
		return $output;
	}

	/**
	 * Show status of the item
	 *
	 * @return unknown
	 */
	function showStatus()
	{
		$status = $this->item['status'];

		if($status == UC_STATUS_PENDING)
			return "Pending";
		if($status == UC_STATUS_APPROVED)
			return "Approved";
		if($status == UC_STATUS_SPAM)
			return "Marked as Spam";
	}
}
?>